<template>
    <div class="pageContainer">
        <el-container class="bg-white rounded-lg">
            <el-header style="border-bottom: 1px solid #eeeeee; height: 110px;">
                <div>
                    <el-form :model="searchform" label-width="80px" class="mb-3 flex justify-between items-center pt-5">
                        <el-form-item label="关键词" class="mb-0">
                            <el-input v-model="searchform.keyword" clearable></el-input>
                        </el-form-item>
                        <el-form-item class="mb-0">
                            <el-button type="primary" @click="onSearch">搜索</el-button>
                            <el-button @click="onClearSearch">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="flex items-center justify-between">
                    <el-button type="primary" @click="onAdd">新增</el-button>
                    <el-button type="primary" text @click="onSearch">
                        <el-icon :size="20">
                            <Refresh />
                        </el-icon>
                    </el-button>
                </div>
            </el-header>
            <el-container>
                <div class="w-full">
                    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column label="序号" width="90" align="center" fixed="left" prop="rank">
                            <template #default="{ $index, row }">
                                {{ (currentPage - 1) * limit + $index + 1 }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="username" label="管理员" min-width="200">
                            <template #default="{ $index, row }">
                                <div class="flex items-center justify-start">
                                    <el-avatar :size="50" :src="row.avatar" />
                                    <div class="flex flex-col text-xs mx-3">
                                        <span style="margin-bottom: 5px;">{{ row.username }}</span>
                                        <span>ID: {{ row.id }}</span>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="role_id" label="所属角色" min-width="200">
                            <template #default="{ $index, row }">
                                {{ row.role.name }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="status" label="状态" min-width="200">
                            <template #default="{ $index, row }">
                                <el-switch v-model="row.status" :active-value="1" :inactive-value="0"
                                    @change="onStatusChange(row.id, $event)" />
                            </template>
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" min-width="120" align="center">
                            <template #default="scope">
                                <el-button link type="primary" @click="onEdit(scope.row)">修改</el-button>
                                <el-popconfirm title="确定删除该管理员吗？" @confirm="onDelete(scope.row)">
                                    <template #reference>
                                        <el-button type="primary" link>删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div style="height: 50px;" class="flex items-center justify-center">
                        <el-pagination layout="prev, pager, next" background :current-page="currentPage"
                            :total="totalCount" :page-size="10" @current-change="onPageChange" />
                    </div>
                </div>
            </el-container>
        </el-container>
        <FormDrawer ref="drawerRef" :title="drawerTitle" @submit="onSubmit">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名" clearable />
                </el-form-item>
                <el-form-item label="密码" prop="password" v-if="form.id == 0">
                    <el-input v-model="form.password" placeholder="请输入密码" type="password" show-password>
                        <template #prefix>
                            <el-icon :size="20">
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item label="头像" prop="avatar">
                    <AvatarSelector v-model="form.avatar" />
                </el-form-item>
                <el-form-item label="所属角色" prop="role_id">
                    <el-select v-model="form.role_id" placeholder="请选择所属角色" style="width: 240px">
                        <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </FormDrawer>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { getManagerList, updateManagerById, addManager, deleteManagerById, UpateManagerStatusById } from '../../api/manager'
import { toast, openFullScreenLoading, closeFullScreenLoading } from '../../composables/util'
import FormDrawer from '../../components/FormDrawer.vue'
import AvatarSelector from '../../components/AvatarSelector.vue'
import { useTableSearch, useForm } from '../../composables/common'

// 定义查询表单
const searchform = reactive({
    keyword: "",
})

// 查询数据成功后的回调，可以不用定义
const onGetDataSucceed = (res) => {
    roles.value = res.data.roles
}

const roles = ref(null)

const {
    tableRowClassName,
    onSearch,
    getData,
    onClearSearch,
    onPageChange,
    currentPage,
    tableData,
    totalCount,
    limit
} = useTableSearch({
    searchform,
    getTableList: getManagerList,
    onGetDataSucceed // 非必须
})



const onStatusChange = (id, status) => {
    const loading = openFullScreenLoading()
    UpateManagerStatusById(id, status).then(res => {
        toast("修改管理员成功")
    }).finally(() => {
        closeFullScreenLoading(loading)
    })
}

// 表单,注意表单一定需要一个id字段
const form = reactive({
    id: 0,
    username: '',
    password: '',
    role_id: 0,
    status: null,
    avatar: ''
})
// 表单规则
const rules = {
    username: [{ required: true, message: '用户不能为空', trigger: 'blur' }],
    password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
    role_id: [{ required: true, message: '角色id不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
    avatar: [{ required: true, message: '图像不能为空', trigger: 'blur' }],
}

const {
    formRef,
    drawerRef,
    drawerTitle,
    onAdd,
    onEdit,
    onDelete,
    onSubmit
} = useForm({
    title: '管理员', // 功能名称
    form, // form表单，必须
    tableData, // 表单数据来源
    getData, // 更新表单数据接口
    add: addManager, // 新增接口
    update: updateManagerById, // 修改接口
    delete: deleteManagerById // 删除接口
})

</script>
<style scoped>
:deep(.el-table .cell) {
    height: 50px;
    line-height: 50px;
}
</style>